package tpo.wmb.consumer.controller;

import com.github.pagehelper.PageInfo;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser;
import dto.Dto;
import dto.ShowWares;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import pojo.BuyOrder;
import pojo.BuyReturn;
import pojo.DetailList;
import pojo.User;
import tpo.wmb.consumer.service.OrderService;
import tpo.wmb.consumer.service.UserService;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("deprecation")
@RestController
public class OrderController {

    @Resource
    private OrderService orderService;

    @Resource
    private UserService userService;


    /**
     * @param pageNum 当前页
     * @param pageControl 翻页控制
     * @param code 单号以及客户名
     * @param orderStaId 单据状态
     * @param sort 订单列表分类查询(1:全部订单查询; 2:待审核订单查询; 3:待收款订单)
     * @return
     */
    @RequestMapping("/SelectOrderList")
    public PageInfo<BuyOrder> selectOrderList(@RequestParam(value = "pageNum",required = false) Integer pageNum,
                                    @RequestParam(value = "pageControl",required = false) String pageControl,
                                    @RequestParam(value = "code",required = false) String code,
                                    @RequestParam(value = "orderStaId",required = false) Integer orderStaId,
                                              @RequestParam(value = "sort",required = false) Integer sort,
                                              @RequestParam(value = "pageSize0",required = false) Integer pageSize0){
        Integer pageSize = 5;
        if(pageSize0 != null) {
            if (pageSize0 == -1) {
                pageSize = 1000;
            }
        }
        //先加条件map
        Map<String, Object> map1 = new HashMap<String, Object>();
        map1.put("pageNum", pageNum);
        map1.put("pageSize", pageSize);
        map1.put("code", code);
        map1.put("orderStaId", orderStaId);
        if(sort != null){
            if(sort==2){//待审核订单(7)查询
                map1.put("orderStaId", 7);
            }else if(sort==3){//待收款订单
                map1.put("recStaId", 12);
            }
        }
//        code还要模糊查询客户名称得到id
        if(code != null && code != "") {
            List<Integer> list = userService.selectUserListInt(code);
            if(list != null && list.size()!=0) {
                map1.put("list", list);
            }
        }

        //再用条件查询分页
        Dto<PageInfo<BuyOrder>> dto0 = orderService.selectOrderList0(map1);
        // 再用分页信息,翻页判断比较,改变条件
        if ("pre".equals(pageControl)) {
            pageNum = pageNum - 1 > 0 ? pageNum - 1 : pageNum;
        } else if ("next".equals(pageControl)) {
            pageNum = pageNum + 1 <= dto0.getObj().getPages() ? pageNum + 1 : pageNum;
        }
//        改变条件
        map1.put("pageNum", pageNum);
        //再用新改的条件条件查询分页
        dto0 = orderService.selectOrderList0(map1);

        return dto0.getObj();
    }


    @RequestMapping("/SelectOrder")
    public BuyOrder selectOrder1(@RequestParam Integer id){
        Dto<BuyOrder> dto = orderService.selectOrder0(id);
        return dto.getObj();
    }


    @RequestMapping("/UpdateOrder")
    public Integer updateOrder(@RequestBody BuyOrder buyOrder){
        return orderService.updateOrder0(buyOrder);
    }


    @RequestMapping("/UpdateOrder2")
    public Integer updateOrder2(@RequestBody BuyOrder buyOrder){
        return orderService.updateOrder2(buyOrder);
    }


    @RequestMapping("/InsertOrder")
    public String insertOrder(@RequestBody BuyOrder buyOrder){
        return orderService.insertOrder0(buyOrder);
    }


    @RequestMapping("/DeleteOrder")
    public Integer deleteOrder(@RequestParam Integer id){
        return orderService.deleteOrder0(id);
    }


    /**
     *
     * @param pageNum  页码
     * @param pageControl 翻页控制
     * @param code 单号/客户名搜索框 模糊查询
     * @param orderSta 退货单状态
     * @param type 退货类型
     * @param returnDateHead 时间搜索范围 头
     * @param returnDateEnd 时间搜索范围 尾
     * @return
     */
    @RequestMapping("/SelectBuyReturnList")
    public PageInfo<BuyReturn> selectBuyReturnList(@RequestParam Integer pageNum,
                                                   @RequestParam(required = false) String pageControl,
                                                   @RequestParam(required = false) String code,
                                                   @RequestParam(required = false) Integer type,
                                                   @RequestParam(required = false) Integer orderSta,
                                                   @RequestParam(required = false) String returnDateHead,
                                                   @RequestParam(required = false) String returnDateEnd){

        Integer pageSize = 5;
        Map<String, Object> map1 = new HashMap<String, Object>();
        map1.put("pageNum", pageNum);
        map1.put("pageSize", pageSize);
        map1.put("code", code);
        map1.put("orderSta", orderSta);
        map1.put("returnDateHead",returnDateHead);
        map1.put("returnDateEnd",returnDateEnd);
        map1.put("type",type);

//        code还要模糊查询客户名称得到id
//        map1.put("list","??");
        //        code还要模糊查询客户名称得到id
        if(code != null && code != "") {
            List<Integer> list = userService.selectUserListInt(code);
            if(list != null && list.size()!=0) {
                map1.put("list", list);
            }
        }


        PageInfo<BuyReturn> pageInfo = orderService.selectBuyReturnList(map1);

        //          翻页判断比较
        if ("pre".equals(pageControl) && pageInfo.isHasPreviousPage()) {
            pageNum = pageNum - 1;
        } else if ("next".equals(pageControl) && pageInfo.isHasNextPage()) {
            pageNum = pageNum + 1;
        }
        map1.put("pageNum", pageNum);
        return orderService.selectBuyReturnList(map1);
    }


    @RequestMapping("/SelectBuyReturn")
    public BuyReturn selectBuyReturn(@RequestParam Integer id){
        return orderService.selectBuyReturn(id);
    }

    @RequestMapping("/UpdateBuyReturn")
    public Integer updateBuyReturn(@RequestBody BuyReturn buyReturn){
        return orderService.updateBuyReturn(buyReturn);
    }


    @RequestMapping("/InsertBuyReturn")
    public String insertBuyReturn(@RequestBody BuyReturn buyReturn){
        return orderService.insertBuyReturn(buyReturn);
    }


    @RequestMapping("/DeleteBuyReturn")
    public Integer deleteBuyReturn(@RequestParam Integer id){
        return orderService.deleteBuyReturn(id);
    }


    /**
     * 根据订单号联合查询商品到 ShowWares 清单对象
     * @param orderId   订单号 或者 退货单号
     * @param type 对应    1   或者   2
     * @return
     */
    @RequestMapping("/SelectDetailList")
    public List<ShowWares> selectDetailList(@RequestParam String orderId, @RequestParam Integer type){
        return orderService.selectDetailList(orderId,type);
    }

    /**
     * 根据退货单号联合查询商品到 ShowWares 清单对象
     * @param orderId
     * @param type
     * @return
     */
    @RequestMapping("/SelectDetailList2")
    public List<ShowWares> selectDetailList2( String orderId, Integer type ){
        return orderService.selectDetailList2(orderId,type);
    }


    /**
     * 新增
     * @param detailList
     * @return
     */
    @RequestMapping("/InsertDetailList")
    public Integer insertDetailList(@RequestBody DetailList detailList){
        return orderService.insertDetailList(detailList);
    }


    /**
     * 根据退货单id显示退货单页面
     * @param id
     * @return
     * name 客户名, reAmount 退款金额, returnDate 退货日期, remark 备注,
     * accessory 附件, code 退货单号, wid 仓库id, wName 仓库名
     */
    @RequestMapping("/SeeUpdateBuyReturn")
    public Map<String,Object> seeUpdateBuyReturn(@RequestParam Integer id){
        Map<String,Object> map = orderService.seeUpdateBuyReturn(id);
        return map;
    }

    /**
     * 根据订单号查出客户
     * @param orderId
     * @return
     */
    @RequestMapping("/OrderUser")
    public User orderUser(@RequestParam Integer orderId){
        return orderService.orderUser(orderId);
    }

    /**
     * 修改订单状态
     * @param orderStaId 订单状态
     * @param recStaId 付款状态
     * @param id id
     * @return
     */
    @RequestMapping("/UpdateOrderState")
    public Integer updateOrderState(@RequestParam(required = false) Integer orderStaId,@RequestParam(required = false) Integer recStaId,@RequestParam(required = false) Integer id){
        return orderService.updateOrderState(orderStaId,recStaId,id);
    }

    /**
     * 查询用户所有订单
     * @param userId
     * @return
     */
    @RequestMapping("/SelectOrderList2")
    public List<BuyOrder> selectOrderList2(@RequestParam Integer userId){
        return orderService.selectOrderList2(userId);
    };


    /**
     * 修改退货单状态
     * @param orderSta 退货单状态
     * @param receSta 付款状态
     * @param id id
     * @return
     */
    @RequestMapping("/UpdateBuyReturnState")
    public Integer updateBuyReturnState(@RequestParam(value = "orderSta",required = false) Integer orderSta,@RequestParam(value = "receSta",required = false) Integer receSta,@RequestParam(value = "id",required = false) Integer id){
        return orderService.updateBuyReturnState(orderSta, receSta, id);
    }



    /**
     * 订单已收款,减去作废的已收款
     * @param code
     * @param integer
     * @return
     */
    @RequestMapping("/UpdateOrder3")
    public Integer updateOrder3(@RequestParam String code,@RequestParam BigDecimal integer){
        return orderService.updateOrder3(code,integer);
    }

    /**
     * 修改退货单
     * @param code
     * @param bigDecimal
     * @return
     */
    @RequestMapping("/UpdateBuyReturn2")
    public Integer updateBuyReturn2(@RequestParam String code,@RequestParam BigDecimal bigDecimal){
        return orderService.updateBuyReturn2(code, bigDecimal);
    }

    /**
     * code查询订货单
     * @param code
     * @return
     */
    @RequestMapping("/SelectOrderCode")
    public BuyOrder selectOrdeCoder(@RequestParam String code){
        return orderService.selectOrdeCoder(code);
    }

    /**
     * code查询退货单
     * @param code
     * @return
     */
    @RequestMapping("/SelectBuyReturn2")
    public BuyReturn selectBuyReturn2(@RequestParam String code){
        return orderService.selectBuyReturn2(code);
    }

    /**
     * 批量修改订单明细
     * @return
     */
    @RequestMapping("/UpdateDetailList2")
    public Integer updateDetailList2(@RequestBody List<DetailList> list){
        return orderService.updateDetailList2(list);
    }


}
